package org.jash.mvp2206.mvp;

import java.lang.ref.WeakReference;

import io.reactivex.rxjava3.disposables.CompositeDisposable;

public class BasePresenter<V> {
//    弱引用
    private WeakReference<V> reference;
    protected CompositeDisposable disposable = new CompositeDisposable();

    public void attach(V v) {
        reference = new WeakReference<>(v);
    }

    public void detach() {
        if (reference != null) {
            reference.clear();
            reference = null;
        }
        if (!disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    public boolean isAttach() {
        return reference != null && reference.get() != null;
    }

    public V getWhoView() {
        return reference != null ? reference.get() : null;
    }

}
